package com.gzhu.knowledgeAdmin.controller;


import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.entity.TDxmbT;
import com.gzhu.knowledgeAdmin.service.TDxmbTService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dxmb")
public class smsTemplateController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    TDxmbTService tDxmbTService;

    //陈
    /**
     * 新增用户信息
     */
    @PostMapping("/add")
    public R add(@RequestBody TDxmbT tDxmbT) {
        System.out.println(tDxmbT);
        try {
            tDxmbTService.insertUser(tDxmbT);
            return R.ok();
        } catch (DuplicateKeyException e) {
            logger.error("Duplicate key error: {}", e.getMessage());
            return R.error();
        } catch (Exception e) {
            logger.error("System error: {}", e.getMessage());
            return R.error();
        }
    }



    /**
     * 全部查询用户信息
     */
    @GetMapping("/selectByAll")
    public R selectByAll(
            @RequestParam("fmbmc") String fmbmc,
            @RequestParam(value = "fqm", required = false) String fqm,
            @RequestParam(value = "fnr", required = false) String fnr,
            @RequestParam(value = "fzt", required = false) String fzt,
            @RequestParam(value = "fmblx", required = false) String fmblx,
            @RequestParam(value = "fdxlx", required = false) String fdxlx) {
        try {
            // 调用 tDxmbTService 中的查询方法，根据多个条件查询数据
            List<TDxmbT> tDxmbTList = tDxmbTService.selectByAll(fmbmc, fqm, fnr, fzt, fmblx, fdxlx);
            return R.ok(tDxmbTList);
        } catch (Exception e) {
            logger.error("系统错误: {}", e.getMessage());
            return R.error();
        }
    }


    /**
     * 通过ID查询用户信息
     */
    @GetMapping("/getByAll/{fId}")
    public R getById(@PathVariable long fId) {
        try {
            TDxmbT tDxmbT = tDxmbTService.getUserById(fId);
            return R.ok(tDxmbT);
        } catch (Exception e) {
            logger.error("System error: {}", e.getMessage());
            return R.error();
        }
    }

    /**
     * 查询所有用户信息
     */
    @GetMapping("/getAll")
    public R getAll() {
        try {
            List<TDxmbT> tDxmbTList = tDxmbTService.getAllUsers();
            return R.ok(tDxmbTList);
        } catch (Exception e) {
            logger.error("System error: {}", e.getMessage());
            return R.error();
        }
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    public R update(@RequestBody TDxmbT tDxmbT) {
        try {
            tDxmbTService.updateUser(tDxmbT);
            return R.ok();
        } catch (Exception e) {
            logger.error("System error: {}", e.getMessage());
            return R.error();
        }
    }

    /**
     * 通过ID删除用户信息
     */
    @DeleteMapping("/delete/{fId}")
    public R deleteById(@PathVariable long fId) {
        try {
            tDxmbTService.deleteUserById(fId);
            return R.ok();
        } catch (Exception e) {
            logger.error("System error: {}", e.getMessage());
            return R.error();
        }
    }


}
